package cn.mgm.base;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * jdbc 快速上手
 */
public class JDBCQuick {
    public static void main(String[] args) throws Exception {

        // 1. 注册驱动(jdk6之后可不写)
      //  Class.forName("com.mysql.cj.jdbc.Driver");

        // 2. 获取连接对象
        String  url="jdbc:mysql://139.224.197.217:3336/guigu-auth";
        String username="root";
        String password ="kaa3akv8";
        Connection connection = DriverManager.getConnection(url, username, password);

        // 3. 获取 SQL 执行语句对象
        Statement statement = connection.createStatement();


        // 4. 执行 SQL 语句并执行
        String sql ="SELECT id,username,password,phone,name FROM `sys_user` LIMIT 0,1000";
        ResultSet resultSet = statement.executeQuery(sql);

        // 5.处理结果,遍历 resultSet 结果集
        while (resultSet.next()){
            Long Id = resultSet.getLong("id");
            String name = resultSet.getString("name");
            String user_name = resultSet.getString("username");
            System.out.println(Id+"\t"+name+"\t"+user_name);
        }

        // 6. 释放资源(先开后关)
        resultSet.close();
        statement.close();
        connection.close();

    }
}